package com.cbs.android.component.push.jpush;

import android.content.Context;

import com.cbs.android.component.push.pushinterface.LocalNotification;
import com.cbs.android.component.push.pushinterface.PushHandler;
import com.cbs.android.component.push.pushinterface.PushInterface;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.data.JPushLocalNotification;

/**
 * Created by broche on 11/9/15.
 */
public class JPush implements PushInterface {
    private static JPush instance = null;
    private Context context;
    private List<PushHandler> handlers = new ArrayList<>();

    public static List<PushHandler> getHandlers() {
        if (instance != null) {
            return instance.handlers;
        } else {
            return null;
        }
    }

    public JPush(Context context) {
        instance = this;
        this.context = context;
        JPushInterface.init(context);
    }

    @Override
    public void addLocalNotification(LocalNotification localNotification) {
        JPushLocalNotification notification = new JPushLocalNotification();
        notification.setBuilderId(0);
        notification.setNotificationId(localNotification.getNotificationId());
        notification.setTitle(localNotification.getTitle());
        notification.setContent(localNotification.getContent());
        notification.setExtras(localNotification.getExtra());
        notification.setBroadcastTime(localNotification.getTimestamp());

        JPushInterface.addLocalNotification(context, notification);
    }

    @Override
    public void clearLocalNotification() {
        JPushInterface.clearAllNotifications(context);
    }

    @Override
    public void removeLocalNotification(long notificationId) {
        JPushInterface.removeLocalNotification(context, notificationId);
    }

    @Override
    public void addPushHandler(PushHandler handler) {
        this.handlers.add(handler);
    }

    public void setDebug(boolean debug) {
        JPushInterface.setDebugMode(debug);
    }
}
